package com.example.testhiber.initPackage.business.controller;


import com.example.testhiber.initPackage.business.utils.RandomValidateCodeUtil;
import com.example.testhiber.initPackage.business.utils.StringKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;

@Controller
public class HelloController {
    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
    private static final String PATH="/test/";

    @Autowired
    private HttpServletRequest request;

    /**
     * 首页跳转
     * @return
     */
    @ResponseBody
    @RequestMapping("/index")
    public ModelAndView sayHello(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName(PATH+"index");
        mav.addObject("test","test11634165");
        return mav;
    }

    /**
     * 二维码生成
     * @return
     */
    @ResponseBody
    @RequestMapping("/ewmsc")
    public ModelAndView ewmsc(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName(PATH+"ewmsc");
        mav.addObject("test","test11634165");
        return mav;
    }

    /**
     * Js实现内容向上无缝循环滚动
     * @return
     */
    @ResponseBody
    @RequestMapping("/gdxg")
    public ModelAndView gdxg(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName(PATH+"gdxg2");
        mav.addObject("test","test11634165");
        return mav;
    }


    /**
     * 生成验证码
     */
    @RequestMapping(value = "/getVerify")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
            response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
            randomValidateCode.getRandcode(request, response);//输出验证码图片方法
        } catch (Exception e) {
            logger.error("获取验证码失败>>>>   ", e);
        }
    }


    @ResponseBody
    @RequestMapping("/login")
    public ModelAndView login(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName(PATH+"login");
        String res = StringKit.toString(request.getSession().getAttribute("RANDOMVALIDATECODEKEY"));
        mav.addObject("test","test2");
        mav.addObject("res",res);
        return mav;
    }
    @ResponseBody
    @RequestMapping("/logi")
    public ModelAndView logi(ModelAndView modelAndView){
        modelAndView.setViewName("redirect:http://47.56.160.142:8888/");
//        ScreenshotTool screenshotTool =new ScreenshotTool("d:\\Hello","jpg");


        new Thread(){


                }.start();

//        ScreenshotTool screenshotTool=new ScreenshotTool("d:\\Hello","jpg");
//        screenshotTool.snapShot();
//        screenshotTool.SendEmail("","1923232722@qq.com","1923232722@qq.com","lwsqtqtemfmkcdbc");




        /*screenshotTool.SendEmail("","491081879@qq.com","1923232722@qq.com","lwsqtqtemfmkcdbc");*/
        return modelAndView;
    }


    @ResponseBody
    @RequestMapping("/logi2")
    public void logi2(){
//        modelAndView.setViewName("redirect:http://47.56.160.142:8888/");
//        ScreenshotTool screenshotTool =new ScreenshotTool("d:\\Hello","jpg");
//        screenshotTool.snapShot();

        try {
            //此方法仅适用于JdK1.6及以上版本
            Desktop.getDesktop().browse(
                    new URL("http://47.56.160.142:8888/").toURI());
            Robot robot = new Robot();
            robot.delay(2000);
            Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
            int width = (int) d.getWidth();
            int height = (int) d.getHeight();
            //最大化浏览器
            robot.keyRelease(KeyEvent.VK_F11);
            robot.delay(2000);
            Image image = robot.createScreenCapture(new Rectangle(0, 0, width,
                    height));
            BufferedImage bi = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics g = bi.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);
            //保存图片
            ImageIO.write(bi, "jpg", new File("D://456.jpg"));

//            ScreenshotTool screenshotTool=new ScreenshotTool("d:\\Hello","jpg");
//            screenshotTool.snapShot();
//        screenshotTool.SendEmail("","1923232722@qq.com","1923232722@qq.com","lwsqtqtemfmkcdbc");
//
            /*screenshotTool.SendEmail("","491081879@qq.com","1923232722@qq.com","lwsqtqtemfmkcdbc");*/
        }catch (Exception e){

        }

    }
}